﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;

namespace FrameDetectionTest
{
    public partial class LoadProject : Form
    {
        string ListPath = string.Empty;
        string PrjPath = string.Empty;
        public string templatePath = string.Empty;
        public string CurrentProjectFilePath { private set; get; }
        public string CurrentProjectFileName { private set; get; }
        public LoadProject(string listpath,string prjpath)
        {
            ListPath = listpath + "\\";
            PrjPath = prjpath + "\\";
            InitializeComponent();
        }
        private void LoadProject_Load(object sender, EventArgs e)
        {
            try
            {
                SetProjectsNamesToListBox();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void SetProjectsNamesToListBox()
        {
            try
            {
                listBoxProjectsList.Items.Clear();

                string[] ProjectsNames = GetProjectsFormListFile(ListPath);

                foreach (string name in ProjectsNames)
                {
                    if (name.Length > 0)
                    {
                        listBoxProjectsList.Items.Add(name);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(ex.Message));
            }
        }
        private string[] GetProjectsFormListFile(string lstDirectory)
        {
            List<string> projects = new List<string>();
            string sLine;
            try
            {
                string sPath = lstDirectory + "\\list.lst";
                if (!File.Exists(sPath))
                {
                    throw new Exception(string.Format("List file not exists: {0}", sPath));
                }

                StreamReader sr = new StreamReader(sPath, Encoding.Default);
                sLine = sr.ReadLine();
                while (sLine != null)
                {
                    if (sLine.Contains("MN"))
                    {
                        projects.Add(sLine);
                    }
                    sLine = sr.ReadLine();
                }
                sr.Close();
                return projects.ToArray();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string GetModelName(string item)
        {
            if (item.Contains("="))
            {
                return item.Split('=')[1].Trim();
            }
            else
            {
                return item.Trim();
            }
        }
        private void listBoxProjectsList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (listBoxProjectsList.SelectedIndex >= 0)
                {
                    CurrentProjectFileName = GetModelName(listBoxProjectsList.Text);//取等号后的
                    string ProjectFilePath = PrjPath + "prj\\" + GetModelName(listBoxProjectsList.Text) + ".json";
                    if (File.Exists(ProjectFilePath))
                    {
                        CurrentProjectFilePath = ProjectFilePath;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        throw new Exception(string.Format("未找到目录选取的对应工程文件"));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}", ex.Message), "Load Project File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void listBoxProjectsList_MouseClick(object sender, MouseEventArgs e)
        {
            string BasePrj;
            if (listBoxProjectsList.SelectedItem.ToString().Contains('='))
            {
                BasePrj = listBoxProjectsList.SelectedItem.ToString().Split('=')[1].Trim();
            }
            else
            {
                BasePrj = listBoxProjectsList.SelectedItem.ToString().Trim();
            }
            //根据BasePrj找到对应机种的图片及选框
            PrjInformation.Clear();
            PrjInformation.AppendText(BasePrj + "\n");
            if(File.Exists(PrjPath + "pic\\" + BasePrj + ".bmp"))
            {
                PrjPictureBox.Image = new Bitmap(PrjPath + "pic\\" + BasePrj + ".bmp");
            }
            else
            {
                PrjInformation.AppendText("机种图片不存在 \n");
                PrjPictureBox.Image = null;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string prjPath;
            OpenFileDialog fileDialog = new OpenFileDialog();
            if(fileDialog.ShowDialog() == DialogResult.OK)
            {
                templatePath = fileDialog.FileName;
            }
            if (File.Exists(templatePath))
            {
                textBox1.Text = templatePath;
                TemplatePictureBox.Image = new Bitmap(templatePath);
            }
        }
    }
}
